﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using IntfClass;
using System.Data.SqlClient;


namespace IntfSetting
{
    public partial class FrmWebServiceNew : Form
    {
        public FrmWebServiceNew(string ID, string Code, string Name, DataTable datGridView)
        {
            InitializeComponent();
            datParent = datGridView;

            if (!(Code.Equals(string.Empty)))
            {
                txtCode.Text = Code;
                txtName.Text = Name;
                strID = ID;
                datSource = datGridView.Select("ID = " + ID);
                blnNew = false;


            }
        }

        private DataTable datGridView = new DataTable();
        private DataRow[] datSource = new DataRow[1];
        private DataTable datParent = new DataTable();

        private Boolean blnNew = true;
        private string strID = string.Empty;
        private void btnNew_Click(object sender, EventArgs e)
        {
            FrmSetWebService frmSetWebService = new FrmSetWebService(string.Empty,datGridView);
            frmSetWebService.ShowDialog();
            //SearchGridView();

        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckData())
            {
                return;
            }

            Int64 intId = 0;
            StringBuilder strSql = new StringBuilder();
            ClassDB classDB = new ClassDB();

            SqlConnection conn = classDB.CreateConnection();
            conn.Open();
            
            SqlParameter sqlParCode = new SqlParameter("Code", SqlDbType.NVarChar, 50);
            sqlParCode.Value = txtCode.Text.Trim();
            SqlParameter sqlParName = new SqlParameter("Name", SqlDbType.NVarChar, 50);
            sqlParName.Value = txtName.Text.Trim();

            SqlParameter[] sqlParameters = { sqlParCode, sqlParName };
            SqlTransaction sqlTransaction = conn.BeginTransaction();

            try
            {
                if (blnNew)
                {
                    strSql.AppendLine("Insert into Tside( Code, Name) values (@Code,@Name)");
                    intId = classDB.ExecuteID(strSql.ToString(), conn, sqlTransaction, sqlParameters);
                }
                else
                {
                    strSql.AppendLine("Update Tside set Code = @Code , Name = @Name");
                    strSql.AppendLine("Where ID = @ParentID");

                    SqlParameter sqlParParentID = new SqlParameter("ParentID", SqlDbType.Int);
                    sqlParParentID.Value = Convert.ToInt64(strID);

                    classDB.ExecuteNone(strSql.ToString(), conn, sqlTransaction, new SqlParameter[] { sqlParCode, sqlParName, sqlParParentID });

                    strSql = new StringBuilder();
                    strSql.AppendLine("Delete from TSideList where ParentID = @ParentID");

                    classDB.ExecuteNone(strSql.ToString(), conn, sqlTransaction, new SqlParameter[] { sqlParParentID });
                    intId = Convert.ToInt64(strID);
                }

                for (int i = 0; i < datGridView.Rows.Count; i++)
                {
                    datGridView.Rows[i]["ParentID"] = intId;
                }

                classDB.ExecuteBulkInsert("TSideList", datGridView, conn, sqlTransaction);

                sqlTransaction.Commit();

                MessageBox.Show("保存成功！");
                this.Dispose();


            }
            catch (Exception ex)
            {
                sqlTransaction.Rollback();

                MessageBox.Show(ex.Message);

            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }

            
        }

        private Boolean CheckData()
        {
            try
            {
                Boolean blnReturn = true;
                if (txtCode.Text.Trim().Equals("DCS") || txtCode.Text.Trim().Equals("DMS"))
                {
                    MessageBox.Show("编号不可为DCS或DMS");
                    txtCode.Focus();
                    return false;
                }

                if (txtCode.Text.Trim().Equals(string.Empty))
                {
                    MessageBox.Show("编号不可为空");
                    txtCode.Focus();
                    return false;
                }

                if (txtName.Text.Trim().Equals(string.Empty))
                {
                    MessageBox.Show("名称不可为空");
                    txtName.Focus();
                    return false;
                }

                if (datGridView.Rows.Count == 0)
                {
                    MessageBox.Show("WebService不可为空");
                    btnNew.Focus();
                    return false;
                }

                return blnReturn;

            }
            catch (Exception)
            {
                return false;
                throw;
            }
        }

        private void FrmWebServiceNew_Load(object sender, EventArgs e)
        {
            if (blnNew)
            {
                strID = "''";
            }
            else
            {
                this.txtCode.ReadOnly = true;
            }

            string strSql = "select t.ID,t.ParentID,t.SysCode,t.Address from TSideList t where t.ParentID = " + strID;
            ClassDB classDB = new ClassDB();
            datGridView = classDB.ExecuteDataTable(strSql);

            dataGridView1.DataSource = datGridView;

            dataGridView1.Columns[0].Visible = false;
            dataGridView1.Columns[1].Visible = false;
            dataGridView1.Columns[2].Visible = true;
            dataGridView1.Columns[3].Visible = true;

            dataGridView1.Columns[2].ReadOnly = true;
            dataGridView1.Columns[3].ReadOnly = true;

            dataGridView1.Columns[2].HeaderText = "系统编号";
            dataGridView1.Columns[3].HeaderText = "URL";
            dataGridView1.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;

                
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (!(this.dataGridView1.SelectedRows[0].Cells["ID"].Value == null))
            {
                datGridView.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                //dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                //SearchGridView();
            }
        }

        private void btnImp_Click(object sender, EventArgs e)
        {
            if (ofdCSV.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            //csv文件读取
            DataTable dt = ClassCSV.ReadCsv(ofdCSV.FileName);
            //DataSet dataSet = ClassCSV.ImportCSV(System.IO.Path.GetDirectoryName(ofdCSV.FileName), System.IO.Path.GetFileName(ofdCSV.FileName));

            Int64 intParentID = 0;
            if (datGridView.Rows.Count > 0)
            {
                intParentID = Convert.ToInt64(datGridView.Rows[0]["ParentID"].ToString());
            }

            //不应该把以前的给删除掉。
            //datGridView.Rows.Clear();

            for(int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dataRow = datGridView.NewRow();
                dataRow["ParentID"] = intParentID;
                dataRow["SysCode"] = dt.Rows[i][0];
                dataRow["Address"] = dt.Rows[i][1];
                datGridView.Rows.Add(dataRow);
            }
            
            dataGridView1.DataSource = datGridView;

        }

        private void btnUpt_Click(object sender, EventArgs e)
        {
            if (!(this.dataGridView1.SelectedRows[0].Cells["ID"].Value == null))
            {
                FrmSetWebService frmSetWebService = new FrmSetWebService(this.dataGridView1.SelectedRows[0].Cells["ID"].Value.ToString(),datGridView);
                frmSetWebService.ShowDialog();
            }

        }

        private void SearchGridView()
        {
            string strSql = "select t.ID,t.ParentID,t.SysCode,t.Address from TSideList t where t.ParentID = " + strID;
            ClassDB classDB = new ClassDB();
            datGridView = classDB.ExecuteDataTable(strSql);

            dataGridView1.DataSource = datGridView;

        }

    }
}
